/*
 * FXGL - JavaFX Game Library. The MIT License (MIT).
 * Copyright (c) AlmasB (almaslvl@gmail.com).
 * See LICENSE for details.
 */

package com.windea.study.kotlin.fxgl.sample.sandbox

import com.almasb.fxgl.animation.*
import com.almasb.fxgl.app.*
import com.almasb.fxgl.dsl.*
import com.almasb.fxgl.entity.*
import javafx.geometry.*
import javafx.scene.input.*
import javafx.scene.paint.*
import javafx.scene.shape.*
import javafx.util.*

/**
 * Shows how to use input service and bind actions to triggers.
 */
class AnimSample : GameApplication() {
	private lateinit var anim: Animation<*>
	
	override fun initSettings(settings: GameSettings) {
		settings.width = 800
		settings.height = 600
		settings.title = "AnimSample"
		settings.version = "0.1"
	}
	
	override fun initInput() {
		onKeyDown(KeyCode.F, "f") {
			anim.onUpdate(0.5)
			
			getGameWorld().entities.forEach { e -> println(e.position) }
		}
		onKeyDown(KeyCode.G, "g") { anim.stop() }
		onKeyDown(KeyCode.Q, "q") { anim.pause() }
		onKeyDown(KeyCode.E, "e") { anim.resume() }
	}
	
	override fun initGame() {
		getGameWorld().addEntityFactory(EFactory())
		
		val e = entityBuilder()
			.at(100.0, 100.0)
			.view(Rectangle(40.0, 40.0, Color.BLUE))
			.buildAndAttach()
		
		anim = animationBuilder()
			.duration(Duration.seconds(2.0))
			.translate(e)
			.from(Point2D(100.0, 100.0))
			.to(Point2D(200.0, 100.0))
			.build()
		anim.start()
	}
	
	class EFactory : EntityFactory {
		@Spawns("block")
		fun newBlock(data: SpawnData): Entity {
			return entityBuilder()
				.from(data)
				.view(Rectangle(40.0, 40.0, Color.BLUE))
				.build()
		}
	}
}


fun main(args: Array<String>) {
	GameApplication.launch(AnimSample::class.java, args)
}

